from wsgiref.util import application_uri

import requests

if __name__ == '__main__':
    ip_url = "http://127.0.0.1:8080"

    #1.request.方法(url)来实现，适合不用配置请求头，cookie类的
    # respose = requests.get("https://cn.bing.com/search?form=bing&q=%E4%BD%A0%E5%A5%BD")
    # print(respose.text)

    #2.使用 requests.Request 类构造请求
    #商务接口:用户登录接口
    method = "POST" #通常建议大写
    url = ip_url + "/dar/user/login"
    headers = {"Content-Type" : "application/x-www-form-urlencoded;charset=UTF-8"}
    #个人认为header指定的是urlencoded表单形式的，就data，json形式的文本，就请求体json
    data = {
        "user_name": "test",
        "passwd": "123456"
    }
    #创建请求对象
    req = requests.Request(method, url, headers=headers, data=data)
    #创建会话,with无论会话正常与否都会关闭会话，释放资源，感觉这些发送请求还是很麻烦的
    with requests.Session() as session:
        #准备请求
        prepared = session.prepare_request(req)
        #发送请求
        response = session.send(prepared)
        #还得是那个百里老师讲的text获取的是原始的数据，而不是json
        print(response.text)